package com.longxi.tianjintianbao.ui.presenter.main;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.model.message.Message;
import com.longxi.tianjintianbao.model.message.MessageListCount;
import com.longxi.tianjintianbao.ui.activity.main.MainActivity;
import com.longxi.tianjintianbao.ui.activity.message.MessageActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.message.MessageViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.base.LazyFragment;
import com.longxi.tianjintianbao.ui.view.main.IMessageFgView;
import com.longxi.tianjintianbao.utils.MyUtils;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MessageFgPresenter extends BasePresenter<IMessageFgView> {

    private MultiTypeAdapter adapter;
    private Items items;
    private boolean isRefresh = false;//页面判断是否要刷新

    public MessageFgPresenter(BaseActivity context, LazyFragment fragment) {
        super(context, fragment);
    }

    public void setBar() {
        mFragment.setTitle("消息");
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(Message.class, new MessageViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void setData(){
        items = new Items();
//        items.add(new Message(1, R.mipmap.msg_fkdj, 3, 0, "访客登记", "", "", true));
        items.add(new Message(2, R.mipmap.msg_wdsp, 3, 0, "我的审批", "", "", true));
//        items.add(new Message(3, R.mipmap.msg_wdrw, 1, 0, "我的任务", "", "", true));
        items.add(new Message(4, R.mipmap.msg_bsbx, 2, 0, "报事报修", "", "", true));
//        items.add(new Message(9, R.mipmap.msg_wpfx, 6, 0, "物品放行", "", "", true));
        items.add(new Message(7, R.mipmap.msg_pzjc, 4, 0, "品质检查", "", "", true));
        items.add(new Message(8, R.mipmap.msg_gzzg, 5, 0, "整改通知", "", "", true));

        adapter.setItems(items);
        adapter.notifyDataSetChanged();

        mFragment.hideLoading();

        getMessageCount();
    }

    //获取信息个数
    public void getMessageCount() {
        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiRetrofit.getInstance().getMessageListCount(object)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MySubscriber<HttpResult<MessageListCount>>(mContext) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable throwable) {
                        mContext.hideWaitingDialog();
                    }

                    @Override
                    public void onNext(HttpResult<MessageListCount> result) {
                        mContext.hideWaitingDialog();
                        MainActivity mainActivity2 = (MainActivity)mContext;
                        int msgCount = 0;
                        if(result.getCode() != 0){
                            return;
                        }
                        if(result.getData().size() == 0){
                            for (int i = 0; i < items.size(); i++) {
                                Message message = (Message) items.get(i);
                                message.setUnreadMessages(0);
                                adapter.notifyDataSetChanged();
                            }
                            mainActivity2.setMsgCount(msgCount);
                            return;
                        }

                        for (int i = 0; i < items.size(); i++) {
                            Message message = (Message) items.get(i);
                            message.setUnreadMessages(0);
                            for (MessageListCount messageCount : result.getData()) {
                                if(message.getRole() == messageCount.getBilltype()){
                                    message.setUnreadMessages(messageCount.getCoun());
                                }
                            }
                            adapter.notifyItemChanged(i);
                        }


                        for (MessageListCount messageCount : result.getData()) {
                            msgCount += messageCount.getCoun();
                        }
                        mainActivity2.setMsgCount(msgCount);
                    }
                });
    }

    AdapterListener listener = new AdapterListener() {
        @Override
        public void onClick(int position) {
            items.get(position);
            Intent intent = new Intent(mContext, MessageActivity.class);
            Message message = (Message)items.get(position);
            intent.putExtra(AppConst.NAME, message.getName());
            intent.putExtra(AppConst.STATE, message.getRole());
            mContext.startActivity(intent);
        }

        @Override
        public void onDelete(int position) {
            MyUtils.showToast("点击了删除");
            adapter.notifyItemRemoved(position);
            items.remove(position);
        }
    };

    public void onDestroy() {
        listener = null;
    }

    public void setRefresh(boolean refresh) {
        isRefresh = refresh;
    }

    public void onResume() {
        if(isRefresh){
            setRefresh(false);
            getMessageCount();
        }
    }
}
